package com.mm.util;

import com.mm.enums.CodeMsgEnu;
import com.mm.statics.CodeMsg;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@ApiModel(value = "返回状态码参数实体", description = "服务端返回的集合")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultUtil<T> {

    @ApiModelProperty(value = "状态码", name = "code", example = "0", required = true)
    private int code;
    @ApiModelProperty(value = "提示信息", name = "msg", example = "success", required = true)
    private String msg;
    @ApiModelProperty(value = "返回消息体", name = "data")
    private T data;
    @ApiModelProperty(value = "返回当前时间戳", name = "currentTime", required = true)
    private long currentTime;

    private Integer type;

    public static <T> ResultUtil<T> success(T data) {
        return new ResultUtil<T>(data, "success", System.currentTimeMillis());
    }


    private ResultUtil(T data, String msg, long currentTime) {
        this.data = data;
        this.msg = msg;
        this.currentTime = currentTime;
    }
    /**
     * 失败时候的调用
     */
    public static <T> ResultUtil<T> error(CodeMsg codeMsg) {
        return new ResultUtil<T>(codeMsg);
    }

    public static <T> ResultUtil<T> error(CodeMsgEnu codeMsg) {
        return new ResultUtil<T>(codeMsg);
    }

    private ResultUtil(CodeMsg codeMsg) {
        if (codeMsg != null) {
            this.code = codeMsg.getCode();
            this.msg = codeMsg.getMsg();
        }
    }

    private ResultUtil(CodeMsgEnu codeMsg) {
        if (codeMsg != null) {
            this.code = codeMsg.getCode();
            this.msg = codeMsg.getMsg();
        }
    }
}
